package com.xuecheng.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.xuecheng.search.config.ElasticsearchProperties;
import com.xuecheng.search.exception.DeleteIndexDocFailedException;
import com.xuecheng.search.exception.SaveIndexDocFailedException;
import com.xuecheng.search.exception.UpdateIndexDocFailedException;
import com.xuecheng.search.po.CourseIndex;
import com.xuecheng.search.service.ICourseIndexService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author 勾新杰
 * @version 1.0
 * @description 课程索引管理接口实现
 * @date 2022/9/25 7:23
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CourseIndexServiceImpl implements ICourseIndexService {

    /**
     * RestHighLevelClient
     */
    private final RestHighLevelClient elasticsearchClient;

    /**
     * elasticsearch配置类
     */
    private final ElasticsearchProperties elasticsearchProperties;

    /**
     * 添加课程索引
     *
     * @param courseIndex 课程索引对象
     */
    @Override
    public Boolean saveCourseIndexDoc(CourseIndex courseIndex) {
        try {
            String jsonStr = JSON.toJSONString(courseIndex);
            IndexRequest indexRequest = new IndexRequest(elasticsearchProperties.getCourse().getIndexName()).id(String.valueOf(courseIndex.getId()));
            indexRequest.source(jsonStr, XContentType.JSON);
            elasticsearchClient.index(indexRequest, RequestOptions.DEFAULT);
            return true;
        } catch (IOException e) {
            log.error("添加课程索引失败：{}", e.getMessage());
            throw new SaveIndexDocFailedException();
        }
    }

    /**
     * 更新课程索引
     *
     * @param courseIndex 课程索引对象
     */
    @Override
    public Boolean updateCourseIndexDoc(CourseIndex courseIndex) {
        try {
            UpdateRequest updateRequest = new UpdateRequest(elasticsearchProperties.getCourse().getIndexName(), String.valueOf(courseIndex.getId()));
            String jsonStr = JSON.toJSONString(courseIndex);
            updateRequest.doc(jsonStr, XContentType.JSON);
            elasticsearchClient.update(updateRequest, RequestOptions.DEFAULT);
            return true;
        } catch (IOException e) {
            log.error("更新课程索引失败：{}", e.getMessage());
            throw new UpdateIndexDocFailedException();
        }
    }

    /**
     * 删除课程索引
     *
     * @param id 课程索引id
     */
    @Override
    public Boolean deleteCourseIndexDoc(String id) {
        try {
            DeleteRequest deleteRequest = new DeleteRequest(elasticsearchProperties.getCourse().getIndexName(), id);
            elasticsearchClient.delete(deleteRequest, RequestOptions.DEFAULT);
            return true;
        } catch (IOException e) {
            log.error("删除课程索引失败：{}", e.getMessage());
            throw new DeleteIndexDocFailedException();
        }
    }
}
